﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;

namespace nanoCMS.Core.Extensions {
	public static class ImageExtensions {
		/// <summary>
		/// 
		/// </summary>
		/// <param name="image"></param>
		/// <param name="width"></param>
		/// <param name="height"></param>
		/// <param name="x"></param>
		/// <param name="y"></param>
		/// <returns></returns>
		public static Image Crop(this Image image, int width, int height, int x, int y) {
			if (width <= image.Width && height <= image.Height) {
				Bitmap bmpImage = image as Bitmap;
				Rectangle cropArea = new Rectangle(x, y, width, height);
				Bitmap bmpCrop = bmpImage.Clone(cropArea, bmpImage.PixelFormat);
				return (Image)(bmpCrop);
			} else {
				return image;
			}
		}

		public static Image BestUsabilityCrop(this Image image, decimal targetRatio) {
			int targetWidth;
			int targetHeight;
			int x, y;

			decimal imageRatio = (decimal)image.Width / (decimal)image.Height;
			if (imageRatio > targetRatio) {
				targetWidth = Convert.ToInt32(Math.Floor(targetRatio * image.Height));
				targetHeight = image.Height;
				y = 0;
				x = ((image.Width - targetWidth) % 2 == 0) ? (image.Width - targetWidth) / 2 : (image.Width - targetWidth - 1) / 2;

			} else if (imageRatio < targetRatio) {
				targetHeight = Convert.ToInt32(Math.Floor(image.Width / targetRatio));
				targetWidth = image.Width;
				x = 0;
				y = ((image.Height - targetHeight) % 2 == 0) ? (image.Height - targetHeight) / 2 : (image.Height - targetHeight - 1) / 2;
			} else {

				return image;
			}

			return image.Crop(targetWidth, targetHeight, x, y);

		}


		public static Image Resize(this Image image, int maxWidth, int maxHeight) {
			try {
				int originalWidth = image.Width;
				int originalHeight = image.Height;

				decimal ratio = 0;
				if (originalHeight > maxHeight) {
					ratio = (decimal)originalHeight / (decimal)maxHeight;
				}
				if (originalWidth > maxWidth) {
					decimal ratio2 = (decimal)originalWidth / (decimal)maxWidth;
					if (ratio < ratio2) ratio = ratio2;
				}

				if (ratio == 0) {
					return image;
				}

				int newWidth = 0;
				int newHeight = 0;
				newHeight = (int)Math.Floor(originalHeight / ratio);
				newWidth = (int)Math.Floor(originalWidth / ratio);

				Bitmap bmPhoto = new Bitmap((int)newWidth, (int)newHeight,
										 PixelFormat.Format32bppPArgb);
				bmPhoto.SetResolution(image.HorizontalResolution, image.VerticalResolution);

				Graphics grPhoto = Graphics.FromImage(bmPhoto);
				grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;

				grPhoto.DrawImage(image,
					 new Rectangle(0, 0, (int)newWidth, (int)newHeight),
					 new Rectangle(0, 0, (int)originalWidth, (int)originalHeight),
					 GraphicsUnit.Pixel);

				grPhoto.Dispose();

				return bmPhoto;
			} catch {
				return image;
			}
		}
	}
}
